﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Text;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Collections;
using Water.Web.Controls.BaseInterface;

[assembly: WebResource("Water.Resources.Default.JS.IconDock.jquery.dock-0.8b.source.js", "text/javascript")]
namespace Water.Web.Controls
{
    [ToolboxData("<{0}:IconDock runat=server></{0}:IconDock>")]
    [ParseChildren(true, "Items")]
    [PersistChildren(false)]
    public class IconDock : WebControl
    {

        private Unit iconMinSide=Unit.Pixel(35);
        private Unit iconMaxSide=Unit.Pixel(70);
        private int distAttDock=100;
        private int coefAttDock=1;
        private int veloOutDock=300;
        private VerticalAlign valign = VerticalAlign.Bottom;
        private Hashtable ht = new Hashtable();

        [Browsable(true), Category("Appearance"), GlobalizeDescription("IconMinSide"), DefaultValue(typeof(Unit),"35")]
        public virtual Unit IconMinSide
        {
            get
            {
                return this.iconMinSide;
            }
            set
            {
                this.iconMinSide = value;
                JSON.JSONSettingUtil.SetHashtable(ht, "iconMinSide", (int)(this.iconMinSide.Value));
            }
        }

        [Browsable(true), Category("Appearance"), GlobalizeDescription("IconMaxSide"), DefaultValue(typeof(Unit), "70")]
        public virtual Unit IconMaxSide
        {
            get
            {

                return this.iconMaxSide;
            }
            set
            {
                this.iconMaxSide = value;
                JSON.JSONSettingUtil.SetHashtable(ht, "iconMinSide", (int)(this.iconMaxSide.Value));
            }
        }


        [Browsable(true), Category("Appearance"), GlobalizeDescription("DistAttDock"), DefaultValue(100)]
        public virtual int DistAttDock
        {
            get
            {
                return this.distAttDock;
            }
            set
            {
                this.distAttDock = value;
                JSON.JSONSettingUtil.SetHashtable(ht, "distAttDock",this.distAttDock);
            }
        }

        [Browsable(true), Category("Appearance"), GlobalizeDescription("CoefAttDock"), DefaultValue(1)]
        public virtual int CoefAttDock
        {
            get
            {
                return this.coefAttDock;
            }
            set
            {
                this.coefAttDock = value;
                JSON.JSONSettingUtil.SetHashtable(ht, "coefAttDock", this.coefAttDock);
            }
        }

        [Browsable(true), Category("Appearance"), GlobalizeDescription("VeloOutDock"), DefaultValue(300)]
        public virtual int VeloOutDock
        {
            get
            {
                return this.veloOutDock;
            }
            set
            {
                this.veloOutDock = value;
                JSON.JSONSettingUtil.SetHashtable(ht, "veloOutDock", this.veloOutDock);
            }
        }

        [Browsable(true), Category("Appearance"), GlobalizeDescription("Valign"), DefaultValue(VerticalAlign.Bottom)]
        public virtual VerticalAlign Valign
        {
            get
            {
                return this.valign;
            }
            set
            {
                this.valign = value;
                if (this.valign != VerticalAlign.NotSet)
                {
                    System.ComponentModel.TypeConverter converter = TypeDescriptor.GetConverter(typeof(VerticalAlign));
                    JSON.JSONSettingUtil.SetHashtable(ht, "valign", converter.ConvertToInvariantString(this.valign).ToLowerInvariant(), true);
                }
            }
        }
 




        private IconItemCollection items;

        [Browsable(true), Category("Data"), GlobalizeDescription("Items"), NotifyParentProperty(true), TypeConverter(typeof(CollectionConverter))]
        [PersistenceMode(PersistenceMode.InnerDefaultProperty)]
        [DesignerSerializationVisibility(DesignerSerializationVisibility.Content)]
        public IconItemCollection Items
        {
            get
            {
                if (this.items == null)
                {
                    this.items = new IconItemCollection();
                }
                return this.items;
            }
        }

        #region override

        protected override HtmlTextWriterTag TagKey
        {
            get
            {
                return HtmlTextWriterTag.Div;
            }
        }

        protected override void OnLoad(EventArgs e)
        {
            base.OnLoad(e);
            PageManager.Register(this);
        }

        protected override void OnPreRender(EventArgs e)
        {
            this.Page.ClientScript.RegisterClientScriptResource(typeof(IconDock), "Water.Resources.Default.JS.IconDock.jquery.dock-0.8b.source.js");

            string defaultSetting = "{iconMinSide : 35,iconMaxSide : 70,distAttDock : 100,coefAttDock : 1,	veloOutDock : 300,valign: 'bottom'}";
            string currentSetting = "{" + JSON.JSONSettingUtil.ToKeyValue(ht) + "}";
            string setting = "$j.extend(" + defaultSetting + "," + currentSetting + ")";
            string js = "$j(function() {$j(\"#" + this.ClientID + "\").addDockEffect(" + setting + ");});\n";
            this.Page.ClientScript.RegisterStartupScript(typeof(IconDock), this.UniqueID, js, true);
            base.OnPreRender(e);
        }

        protected override void AddAttributesToRender(HtmlTextWriter writer)
        {
            if (DesignMode)
                writer.AddAttribute(HtmlTextWriterAttribute.Align, "center");
            base.AddAttributesToRender(writer);
        }
        protected override void RenderContents(HtmlTextWriter writer)
        {
            if (this.items.Count == 0 || this.items == null)
                writer.Write(this.ID);
            else
            {
                foreach (IconItem item in this.items)
                {
                    
                    if (!string.IsNullOrEmpty(item.NavigateUrl))
                    {
                        string _navigateUrl = base.ResolveClientUrl(item.NavigateUrl);
                        writer.AddAttribute(HtmlTextWriterAttribute.Href, _navigateUrl);
                    }
                    if (!string.IsNullOrEmpty(item.Target))
                    {
                        writer.AddAttribute(HtmlTextWriterAttribute.Target, item.Target);
                    }
                    if (!string.IsNullOrEmpty(item.ToolTip))
                    {
                        writer.AddAttribute(HtmlTextWriterAttribute.Title, item.ToolTip);
                    }
                    if (!string.IsNullOrEmpty(item.OnClientClick))
                    {
                        writer.AddAttribute(HtmlTextWriterAttribute.Onclick, item.OnClientClick);
                    }
                    writer.RenderBeginTag(HtmlTextWriterTag.A);
                    if (!string.IsNullOrEmpty(item.ImageUrl))
                    {
                        string _imageUrl = base.ResolveClientUrl(item.ImageUrl);
                        writer.AddAttribute(HtmlTextWriterAttribute.Src, _imageUrl);
                        writer.AddAttribute(HtmlTextWriterAttribute.Border, "0");
                        if (DesignMode)
                        {
                            writer.AddAttribute(HtmlTextWriterAttribute.Width, "35");
                            writer.AddAttribute(HtmlTextWriterAttribute.Height, "35");
                        }
                        writer.RenderBeginTag(HtmlTextWriterTag.Img);
                        writer.RenderEndTag();
                    }
                    writer.RenderEndTag();
                }
            }
        }


        #endregion


    }
}
